package com.gamecollection.minesweeper.view;

import com.gamecollection.mvc.View;
import com.gamecollection.minesweeper.model.MinesweeperModel;
import com.gamecollection.minesweeper.utils.GameUtils;

import java.awt.*;

/**
 * 扫雷游戏视图
 */
public class MinesweeperView implements View {
    
    private MinesweeperModel model;
    
    public MinesweeperView(MinesweeperModel model) {
        this.model = model;
    }
    
    @Override
    public void initialize() {
        // 视图初始化
    }
    
    @Override
    public void render(Graphics g) {
        int state = model.getGameState();
        
        switch (state) {
            case GameUtils.STATE_SELECT_LEVEL:
                renderLevelSelection(g);
                break;
            case GameUtils.STATE_PLAYING:
            case GameUtils.STATE_WIN:
            case GameUtils.STATE_GAME_OVER:
                renderGame(g);
                break;
        }
    }
    
    /**
     * 渲染难度选择界面
     */
    private void renderLevelSelection(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        
        // 获取窗口实际尺寸
        int windowWidth = model.getWidth();
        int windowHeight = model.getHeight();
        
        // 渐变背景 - 使用窗口实际尺寸
        GradientPaint gradient = new GradientPaint(0, 0, new Color(70, 130, 180), 
                                                  windowWidth, windowHeight, new Color(135, 206, 235));
        g2d.setPaint(gradient);
        g2d.fillRect(0, 0, windowWidth, windowHeight);
        
        // 标题 - 居中显示
        g2d.setColor(Color.WHITE);
        g2d.setFont(new Font("微软雅黑", Font.BOLD, 45));
        String title = "选择难度";
        FontMetrics titleFm = g2d.getFontMetrics();
        int titleX = (windowWidth - titleFm.stringWidth(title)) / 2;
        g2d.drawString(title, titleX, 80);
        
        // 按钮宽度和位置计算
        int buttonWidth = 300;
        int buttonHeight = 60;
        int buttonX = (windowWidth - buttonWidth) / 2;
        
        // 简单难度按钮
        drawButton(g2d, buttonX, 150, buttonWidth, buttonHeight, new Color(34, 139, 34), "简单 (9×9, 10雷)");
        
        // 中等难度按钮
        drawButton(g2d, buttonX, 230, buttonWidth, buttonHeight, new Color(255, 165, 0), "中等 (16×16, 40雷)");
        
        // 困难难度按钮
        drawButton(g2d, buttonX, 310, buttonWidth, buttonHeight, new Color(220, 20, 60), "困难 (30×16, 99雷)");
    }
    
    /**
     * 绘制按钮
     */
    private void drawButton(Graphics2D g2d, int x, int y, int width, int height, Color color, String text) {
        // 按钮阴影
        g2d.setColor(new Color(0, 0, 0, 50));
        g2d.fillRoundRect(x + 3, y + 3, width, height, 15, 15);
        
        // 按钮主体
        g2d.setColor(color);
        g2d.fillRoundRect(x, y, width, height, 15, 15);
        
        // 按钮边框
        g2d.setColor(Color.WHITE);
        g2d.setStroke(new BasicStroke(2));
        g2d.drawRoundRect(x, y, width, height, 15, 15);
        
        // 按钮文字
        g2d.setColor(Color.WHITE);
        g2d.setFont(new Font("微软雅黑", Font.BOLD, 28));
        FontMetrics fm = g2d.getFontMetrics();
        int textX = x + (width - fm.stringWidth(text)) / 2;
        int textY = y + (height + fm.getAscent()) / 2;
        g2d.drawString(text, textX, textY);
    }
    
    /**
     * 渲染游戏界面
     */
    private void renderGame(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        
        // 获取窗口实际尺寸
        int windowWidth = model.getWidth();
        int windowHeight = model.getHeight();
        
        // 设置背景颜色 - 更柔和的橙色，完全填充窗口
        g2d.setColor(new Color(255, 218, 185));
        g2d.fillRect(0, 0, windowWidth, windowHeight);
        
        // 绘制信息栏背景 - 调整位置和大小
        g2d.setColor(new Color(245, 245, 245));
        g2d.fillRect(10, 10, windowWidth - 20, 2 * GameUtils.OFFSET - 10);
        
        // 绘制网格线
        drawGridLines(g2d);
        
        // 绘制底层元素
        drawBottomElements(g2d);
        
        // 绘制顶层元素
        drawTopElements(g2d);
        
        // 绘制游戏信息
        drawGameInfo(g2d);
        
        // 绘制状态图标
        drawStatusIcon(g2d);
    }
    
    /**
     * 绘制网格线
     */
    private void drawGridLines(Graphics2D g2d) {
        g2d.setColor(new Color(139, 69, 19));
        g2d.setStroke(new BasicStroke(1.5f));
        
        // 画竖线
        for (int i = 0; i <= model.getMapWidth(); i++) {
            g2d.drawLine(GameUtils.OFFSET + i * GameUtils.SQUARE_LENGTH,
                    3 * GameUtils.OFFSET,
                    GameUtils.OFFSET + i * GameUtils.SQUARE_LENGTH,
                    3 * GameUtils.OFFSET + model.getMapHeight() * GameUtils.SQUARE_LENGTH);
        }
        
        // 画横线
        for (int i = 0; i <= model.getMapHeight(); i++) {
            g2d.drawLine(GameUtils.OFFSET,
                    3 * GameUtils.OFFSET + i * GameUtils.SQUARE_LENGTH,
                    GameUtils.OFFSET + model.getMapWidth() * GameUtils.SQUARE_LENGTH,
                    3 * GameUtils.OFFSET + i * GameUtils.SQUARE_LENGTH);
        }
    }
    
    /**
     * 绘制底层元素
     */
    private void drawBottomElements(Graphics2D g2d) {
        int[][] bottomData = model.getBottomData();
        
        for (int i = 1; i <= model.getMapWidth(); i++) {
            for (int j = 1; j <= model.getMapHeight(); j++) {
                int x = GameUtils.OFFSET + (i - 1) * GameUtils.SQUARE_LENGTH + 1;
                int y = GameUtils.OFFSET * 3 + (j - 1) * GameUtils.SQUARE_LENGTH + 1;
                int size = GameUtils.SQUARE_LENGTH - 2;
                
                if (bottomData[i][j] == -1) {
                    // 地雷
                    g2d.drawImage(GameUtils.lei, x, y, size, size, null);
                } else if (bottomData[i][j] > 0) {
                    // 数字
                    g2d.drawImage(GameUtils.numberImages[bottomData[i][j]], 
                        x + 15, y + 5, null);
                }
            }
        }
    }
    
    /**
     * 绘制顶层元素
     */
    private void drawTopElements(Graphics2D g2d) {
        int[][] topData = model.getTopData();
        
        for (int i = 1; i <= model.getMapWidth(); i++) {
            for (int j = 1; j <= model.getMapHeight(); j++) {
                int x = GameUtils.OFFSET + (i - 1) * GameUtils.SQUARE_LENGTH + 1;
                int y = GameUtils.OFFSET * 3 + (j - 1) * GameUtils.SQUARE_LENGTH + 1;
                int size = GameUtils.SQUARE_LENGTH - 2;
                
                switch (topData[i][j]) {
                    case 0: // 覆盖
                        g2d.drawImage(GameUtils.top, x, y, size, size, null);
                        break;
                    case 1: // 插旗
                        g2d.drawImage(GameUtils.flag, x, y, size, size, null);
                        break;
                    case 2: // 差错旗
                        g2d.drawImage(GameUtils.noflag, x, y, size, size, null);
                        break;
                }
            }
        }
    }
    
    /**
     * 绘制游戏信息
     */
    private void drawGameInfo(Graphics2D g2d) {
        int windowWidth = model.getWidth();
        
        // 信息栏背景 - 使用更精确的尺寸
        g2d.setColor(new Color(240, 240, 240));
        g2d.fillRect(10, 10, windowWidth - 20, 2 * GameUtils.OFFSET - 10);
        
        // 信息栏边框
        g2d.setColor(new Color(180, 180, 180));
        g2d.setStroke(new BasicStroke(2));
        g2d.drawRect(10, 10, windowWidth - 20, 2 * GameUtils.OFFSET - 10);
        
        // 剩余雷数 - 调整位置
        g2d.setColor(new Color(220, 20, 60));
        g2d.setFont(new Font("微软雅黑", Font.BOLD, 28));
        String mineText = "剩余雷数: " + (model.getMineCount() - model.getFlagCount());
        g2d.drawString(mineText, 25, 2 * GameUtils.OFFSET - 20);
        
        // 游戏时间 - 调整位置到右侧
        long gameTime = (model.getEndTime() - model.getStartTime()) / 1000;
        String timeText = "用时: " + gameTime + "秒";
        FontMetrics fm = g2d.getFontMetrics();
        int timeX = windowWidth - fm.stringWidth(timeText) - 25;
        g2d.drawString(timeText, timeX, 2 * GameUtils.OFFSET - 20);
    }
    
    /**
     * 绘制状态图标
     */
    private void drawStatusIcon(Graphics2D g2d) {
        int windowWidth = model.getWidth();
        int centerX = windowWidth / 2 - GameUtils.SQUARE_LENGTH / 2;
        int centerY = GameUtils.OFFSET / 2;
        
        // 图标背景
        g2d.setColor(new Color(255, 255, 255, 150));
        g2d.fillOval(centerX - 5, centerY - 5, GameUtils.SQUARE_LENGTH + 10, GameUtils.SQUARE_LENGTH + 10);
        
        switch (model.getGameState()) {
            case GameUtils.STATE_PLAYING:
                g2d.drawImage(GameUtils.face, centerX, centerY, null);
                break;
            case GameUtils.STATE_WIN:
                g2d.drawImage(GameUtils.win, centerX, centerY, null);
                // 胜利提示
                g2d.setColor(new Color(34, 139, 34));
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 36));
                String winText = "恭喜通关！";
                FontMetrics fm = g2d.getFontMetrics();
                int textX = windowWidth / 2 - fm.stringWidth(winText) / 2;
                g2d.drawString(winText, textX, centerY + 80);
                break;
            case GameUtils.STATE_GAME_OVER:
                g2d.drawImage(GameUtils.over, centerX, centerY, null);
                // 失败提示
                g2d.setColor(new Color(220, 20, 60));
                g2d.setFont(new Font("微软雅黑", Font.BOLD, 36));
                String loseText = "游戏结束";
                FontMetrics fm2 = g2d.getFontMetrics();
                int textX2 = windowWidth / 2 - fm2.stringWidth(loseText) / 2;
                g2d.drawString(loseText, textX2, centerY + 80);
                break;
        }
    }
    
    @Override
    public void update() {
        // 视图更新
    }
    
    @Override
    public void show() {
        // 显示视图
    }
    
    @Override
    public void hide() {
        // 隐藏视图
    }
} 